#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/Fl_Image.H>
#include <FL/Fl_JPEG_Image.H>
#include <FL/FL_PNG_Image.H>
#include <memory>
#include <windows.h>
#include <FL/Fl_ask.H>
#include<winsock.h>
using namespace std;
struct Node{
    int x,y,Num;
};
class Check{
private:
    int Count[8],dir[8][2],Arr[30][30];
    int temp,temp0,temp1,tempdx0,tempdy0, tempdx1,tempdy1,flag,dx0,dy0,dx1,dy1,sum,line;
public:
    Check();
    int there(int x,int y);
    int four(int x,int y);
    int five(int x,int y,int Num);
    int six(int x,int y);
    bool in(int x,int y);
    int judge(int x,int y,int NUM);
}check;
class box:public Fl_Box{
public:
	int handle(int e)
	{
		static int flag =  1;
		switch (e)
		{
			case FL_PUSH:
			if(arr[x][y]||arr[x][y]){
				break;
            }
            arr[x][y]=flag;

           // fl_alert("%d",flag);
            int res=check.judge(x,y,flag);
            if(res==1){
                fl_alert("黑棋胜利");
            }else if(res==2){
                fl_alert("白棋胜利");
            }
			Fl_PNG_Image* pngImg;
			if(flag==1)
				pngImg = new Fl_PNG_Image("img\\3.png"),flag=2;
			else
				pngImg = new Fl_PNG_Image("img\\2.png"),flag=1;
			image(pngImg);
			redraw();
			//fl_alert("%d,%d,%d",x+1,y+1,flag);
			return 1;
		}
	}
	box(int x,int y):x(x), y(y), Fl_Box(x*51,y*51,51,51)
	{
        memset(arr,0,sizeof(arr));
	}
	~box(){

	}
private:
	int x, y;
	int arr[20][20];
};
Check::Check(){
	dir[0][0]=1;dir[0][1]=0;dir[1][0]=1;dir[1][1]=1;dir[2][0]=0;dir[2][1]=1;dir[3][0]=-1;dir[3][1]=1;
	dir[4][0]=-1;dir[4][1]=0;dir[5][0]=-1;dir[5][1]=-1;dir[6][0]=0;dir[6][1]=-1;dir[7][0]=1;dir[7][1]=-1;
	for(int i=0;i<20;i++){
		for(int j=0;j<20;j++){
			Arr[i][j]=0;
		}
	}
}
bool Check::in(int x,int y){
	return x>=0&&x<15&&y>=0&&y<15;
}
int Check::there(int x,int y){
	temp=0;
	for(int i=0;i<4;i++){
		Count[i]=1;
		sum=0;
        dx0=x+dir[i][0];
        dy0=y+dir[i][1];
        dx1=x+dir[i+4][0];
        dy1=y+dir[i+4][1];
        while(in(dx0,dy0)&&Arr[dx0][dy0]==1){
            dx0+=dir[i][0];
            dy0+=dir[i][1];
            Count[i]++;
        }
        while(in(dx1,dy1)&&Arr[dx1][dy1]==1){
            dx1+=dir[i+4][0];
            dy1+=dir[i+4][1];
            Count[i]++;
        }
       if(Count[i]==4){
        	if(((in(dx0,dy0)&&Arr[dx0][dy0]==0)||(in(dx1,dy1)&&Arr[dx1][dy1]==0))){
       			   temp++;
       		}
        }
        else if(Count[i]==3){
           if(Arr[dx1][dy1]==0&&(Arr[dx0][dy0]==0)){
                dx0+=dir[i][0];
            	dy0+=dir[i][1];
            	dx1+=dir[i+4][0];
            	dy1+=dir[i+4][1];
            	if(in(dx0,dy0)&&Arr[dx0][dy0]==1&&in(dx1,dy1)&&Arr[dx1][dy1]==1){
                    temp+=2;
            	}else{
                    temp++;
            	}
            }
        }
        else{
            tempdx0=dx0,tempdy0=dy0;line=0;
            if(Arr[dx1][dy1]==2){
                while(in(dx0,dy0)&&Arr[dx0][dy0]==1){
                    dx0+=dir[i][0];
                    dy0+=dir[i][1];
                    line++;
                }
                line+=Count[i];
                if(line>=4)temp++;
            }else if(in(dx1,dy1)&&Arr[dx1][dy1]==0){
                dx0+=dir[i][0];
                dy0+=dir[i][1];
                while(in(dx0,dy0)&&Arr[dx0][dy0]==1){
                    dx0+=dir[i][0];
                    dy0+=dir[i][1];
                    line++;
                }
                line+=Count[i];
                if(in(dx0,dy0)&&Arr[dx0][dy0]==0)
                    if(line>=3)temp++;
                else{
                    if(line>3)temp++;
                }
            }
            dx0=tempdx0,dy0=tempdy0;
            line=0;
            if(Arr[dx0][dy0]==2){
                while(in(dx0,dy0)&&Arr[dx0][dy0]==1){
                    dx1+=dir[i+4][0];
                    dy1+=dir[i+4][1];
                    line++;
                }
                line+=Count[i];
        	if(line>3)temp++;
            }else if(in(dx0,dy0)&&Arr[dx0][dy0]==0){
                dx1+=dir[i+4][0];
                dy1+=dir[i+4][1];
                while(in(dx1,dy1)&&Arr[dx1][dy1]==1){
                    dx1+=dir[i+4][0];
                    dy1+=dir[i+4][1];
                    line++;
                }
                line+=Count[i];
                if(in(dx1,dy1)&&Arr[dx1][dy1]==0)
                    if(line>=3)temp++;
                else{
                    if(line>3)temp++;
                }
            }
        }
    }
    if(temp>=2)return 1;
    return 0;
}
int Check::four(int x,int y){
	flag=0;
	for(int i=0;i<8;i++)Count[i]=1;
    for(int i=0;i<4;i++){
        dx0=x+dir[i][0];
        dy0=y+dir[i][1];
        dx1=x+dir[i+4][0];
        dy1=y+dir[i+4][1];
        while(in(dx0,dy0)&&Arr[dx0][dy0]==1){
            dx0+=dir[i][0];
            dy0+=dir[i][1];
            Count[i]++;
        }
        while(in(dx1,dy1)&&Arr[dx1][dy1]==1){
            dx1+=dir[i+4][0];
            dy1+=dir[i+4][1];
            Count[i]++;
        }
        if(Count[i]==4){
            if((in(dx0,dy0)&&Arr[dx0][dy0]!=2)||(in(dx1,dy1)&&Arr[dx1][dy1]!=2))
                flag++;
        }
    }
    if(flag>=2)return 1;
    return 0;
}
int Check::five(int x,int y,int Num){
	flag=0;
	for(int i=0;i<8;i++)Count[i]=1;
        for(int i=0;i<4;i++){
            int dx0=x+dir[i][0];
            int dy0=y+dir[i][1];
            int dx1=x+dir[i+4][0];
            int dy1=y+dir[i+4][1];
            while(in(dx0,dy0)&&Arr[dx0][dy0]==Num){
                dx0+=dir[i][0];
                dy0+=dir[i][1];
                Count[i]++;
            }
            while(in(dx1,dy1)&&Arr[dx1][dy1]==Num){
                dx1+=dir[i+4][0];
                dy1+=dir[i+4][1];
                Count[i]++;
            }
            if(Count[i]==5){
                return 1;
            }
            //sum+=Count[i];
        }
        return 0;
}
int Check::six(int x,int y){
	flag=0;
	for(int i=0;i<8;i++)Count[i]=1;
    for(int i=0;i<4;i++){
        int dx0=x+dir[i][0];
        int dy0=y+dir[i][1];
        int dx1=x+dir[i+4][0];
        int dy1=y+dir[i+4][1];
        while(in(dx0,dy0)&&Arr[dx0][dy0]==1){
            dx0+=dir[i][0];
            dy0+=dir[i][1];
            Count[i]++;
        }
        while(in(dx1,dy1)&&Arr[dx1][dy1]==1){
            dx1+=dir[i+4][0];
            dy1+=dir[i+4][1];
            Count[i]++;
        }
        if(Count[i]>5){
            return 1;
        }
    }
    return 0;
}
int Check::judge(int x,int y,int Num){
	Arr[x][y]=Num;
	if(five(x,y,Num)){
		if(Num==1){
			return 1;
         }else{
			 return 2;
          }
	}
	if(Arr[x][y]==1&&(six(x,y)||four(x,y)||there(x,y))){
                return 2;
    }
	return 0;
}
int initPhoto(){
    Fl_Window window(770,765,"五子棋");
	box* pbox;
	for(int i=0;i<15;++i){
		for(int j=0;j<15;++j){
			pbox = new box(i,j);
			pbox->labelsize(18);
			char buf[]="";
			Fl_PNG_Image* pngImg;
			if(i==0&j==0)
				pngImg = new Fl_PNG_Image("img\\4.png");
			else if(i==0&&j>0&&j<14)
				pngImg = new Fl_PNG_Image("img\\19.png");
			else if(i>0&&i<14&&j==0)
				pngImg = new Fl_PNG_Image("img\\16.png");
			else if(i==14&&j>0&&j<14)
				pngImg = new Fl_PNG_Image("img\\25.png");
			else if(i>0&&i<14&&j==14)
				pngImg = new Fl_PNG_Image("img\\22.png");

			else if(i==14&&j==0)
				pngImg = new Fl_PNG_Image("img\\7.png");
			else if(i==0&&j==14)
				pngImg = new Fl_PNG_Image("img\\10.png");
			else if(i==14&&j==14)
				pngImg = new Fl_PNG_Image("img\\13.png");
			else
				pngImg = new Fl_PNG_Image("img\\1.png");
			pbox->image( pngImg );
		}
	}
	window.end();
	window.show();
	return Fl::run();
}

int main(){
    initPhoto();
    return 0;
}
